package com.zy.garbage.service.impl;

import com.zy.garbage.entity.Garbage;
import com.zy.garbage.entity.Sort;
import com.zy.garbage.exception.CommonException;
import com.zy.garbage.form.CreateGarbageForm;
import com.zy.garbage.repository.GarbageRepository;
import com.zy.garbage.repository.SortRepository;
import com.zy.garbage.service.GarbageService;
import com.zy.garbage.util.MyDateUtil;
import com.zy.garbage.vo.GarbageManageVO;
import com.zy.garbage.vo.GarbageVO;
import com.zy.garbage.vo.SortVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class GarbageServiceImpl implements GarbageService {
    @Autowired
    private GarbageRepository garbageRepository;
    @Autowired
    private SortRepository sortRepository;


    @Override
    public GarbageManageVO findAll() {
        List<Garbage> garbageList = garbageRepository.findAll();
        List<SortVO> sortVOList = new ArrayList<>();
        List<Sort> sortList = sortRepository.findAll();
        GarbageManageVO garbageManageVO = new GarbageManageVO();
        HashMap<Integer,Sort> sortHashMap = new HashMap<>();
        for (Sort sort : sortList) {
            sortHashMap.put(sort.getSortId(),sort);
            SortVO sortVO = new SortVO();
            BeanUtils.copyProperties(sort,sortVO);
            sortVOList.add(sortVO);
        }
        garbageManageVO.setSortVOList(sortVOList);
        List<GarbageVO> garbageVOList = new ArrayList<>();
        for (Garbage garbage : garbageList) {
            GarbageVO garbageVO = new GarbageVO();
            BeanUtils.copyProperties(garbage,garbageVO);
            Integer sortId = garbage.getSortId();
            Sort sort = sortHashMap.get(sortId);
            BeanUtils.copyProperties(sort,garbageVO);
            garbageVO.setCreateTime(MyDateUtil.date2String(garbage.getCreateTime()));
            garbageVOList.add(garbageVO);
        }
        garbageManageVO.setGarbageVOList(garbageVOList);
        return garbageManageVO;
    }

    @Override
    public GarbageVO save(CreateGarbageForm createGarbageForm) {
        if (createGarbageForm.getSortId()==null){
            throw new CommonException(500,"分类不能为空");
        }
        if (createGarbageForm.getGarbageName()==null)
            throw new CommonException(500,"名称不能为空");
        Garbage garbage;
        if (createGarbageForm.getGarbageId()!=null){
            Integer garbageId = createGarbageForm.getGarbageId();
            garbage = garbageRepository.findById(garbageId).get();
        } else {
            garbage = new Garbage();
        }
        BeanUtils.copyProperties(createGarbageForm,garbage);
        Garbage garbage1 = garbageRepository.save(garbage);
        GarbageVO garbageVO = new GarbageVO();
        BeanUtils.copyProperties(garbage1,garbageVO);
        return garbageVO;
    }

    @Override
    public void delete(Integer garbageId) {
        if (!garbageRepository.findById(garbageId).isPresent()){
            throw new CommonException(500,"信息不存在");
        }
        garbageRepository.deleteByGarbageId(garbageId);
    }
}
